//
//  FluatAuthCustomBuildModel.m
//  fluat_auth
//
//  Created by 李梓楠 on 2020/9/1.
//

#import "FluatAuthCustomBuildModel.h"

@implementation FluatAuthCustomBuildModel

+ (TXCustomModel *)buildNewFullScreenModel: (NSDictionary *) config selector:(SEL)wxSelector selector:(SEL)appleSelector selector:(SEL)restsClick target: (id) target{
//+ (TXCustomModel *)buildCustomUIModelWith:(NSDictionary *)config{
		TXCustomModel *model = [[TXCustomModel alloc] init];
		
        CGRect rect = [[UIScreen mainScreen] bounds];
        CGSize size = rect.size;
        CGFloat mainScreenWidth = size.width;
    
		//状态栏
		int statusBarStyle = [config[@"statusBarStyle"] intValue];
		switch (statusBarStyle) {
				case 1:
						model.preferredStatusBarStyle = UIStatusBarStyleDefault;
						break;
					case 2:
						model.preferredStatusBarStyle = 	UIStatusBarStyleLightContent;
						break;
				default:
						break;
		}
		model.prefersStatusBarHidden = [config[@"statusBarHidden"] boolValue];
		
		//导航栏
		model.navIsHidden = [config[@"navIsHidden"] boolValue];
		model.navColor = [self colorWithHexString:config[@"navColor"]];

		NSMutableDictionary *navAttributes = [NSMutableDictionary dictionary];
		navAttributes[NSForegroundColorAttributeName] = [self colorWithHexString:config[@"navTitleColor"]];
		navAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:[config[@"navTitleSize"] floatValue]];
		model.navTitle = [[NSAttributedString alloc] initWithString:config[@"navTitle"] attributes:navAttributes];
		model.navBackImage = [UIImage imageNamed:config[@"navBackImage"]];
		model.hideNavBackItem = [config[@"hideNavBackItem"] boolValue];
        model.alertBlurViewAlpha=1;
        model.alertBlurViewColor=[UIColor colorNamed:@"#ffffff"];
		
		//协议详情页
		model.privacyNavColor = [self colorWithHexString:config[@"privacyNavColor"]];
		model.privacyNavTitleColor =[self colorWithHexString:config[@"privacyNavTitleColor"]];
		model.privacyNavTitleFont = [UIFont systemFontOfSize:[config[@"privacyNavTitleSize"] floatValue]];
		model.privacyNavBackImage = [UIImage imageNamed:config[@"privacyNavBackImage"]];
		
        BOOL logoIsHidden=[config[@"logoIsHidden"] boolValue];
        
        if(!logoIsHidden){
            //logo
            model.logoImage =  [UIImage imageNamed:config[@"logoImage"]];
            model.logoFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
                    frame.origin.x = (screenSize.width- [config[@"logoWidth"] doubleValue])/2;
                    frame.origin.y = [config[@"logoOffsetY"] doubleValue];
                    frame.size.width = [config[@"logoWidth"] doubleValue];
                    frame.size.height = [config[@"logoHeight"] doubleValue];
                    return frame;
            };
        }
		
		//slogan
		NSString *text = config[@"sloganText"];
		model.sloganText =  [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName : [self colorWithHexString:config[@"sloganTextColor"]],NSFontAttributeName : [UIFont systemFontOfSize:[config[@"sloganTextSize"] floatValue]]}];
		model.sloganFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
				frame.origin.y = [config[@"sloganOffsetY"] doubleValue];
				return frame;
		};
		
		//phone number
		model.numberFont = [UIFont boldSystemFontOfSize:[config[@"numberSize"] integerValue]];
		model.numberColor = [self colorWithHexString:config[@"numberColor"]];
		model.numberFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
				frame.origin.y = [config[@"numberOffsetY"] doubleValue];
				return frame;
		};
		
		//登录按钮
		model.autoHideLoginLoading = [config[@"loginLoadingHidden"] boolValue];
		NSArray *imageArray = config[@"loginBtnBgImgs"];
		model.loginBtnBgImgs = @[[UIImage imageNamed:imageArray[0]],[UIImage imageNamed:imageArray[1]],[UIImage imageNamed:imageArray[2]]];
		model.loginBtnText =  [[NSAttributedString alloc] initWithString:config[@"loginBtnText"] attributes:@{NSForegroundColorAttributeName : [self colorWithHexString:config[@"loginBtnTextColor"]],NSFontAttributeName : [UIFont boldSystemFontOfSize:[config[@"loginBtnTextSize"] floatValue]]}];
		model.loginBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
				frame.origin.x = [config[@"loginBtnLRPadding"] doubleValue];
				frame.origin.y = [config[@"loginBtnOffsetY"] doubleValue];
				frame.size.height = [config[@"loginBtnHeight"] doubleValue];
				return frame;
		};
		
		//切换其他方式按钮
        model.changeBtnIsHidden = true;
//		model.changeBtnIsHidden = [config[@"changeBtnIsHidden"] boolValue];
//		model.changeBtnTitle = [[NSAttributedString alloc] initWithString:config[@"changeBtnText"] attributes:@{NSForegroundColorAttributeName : [self colorWithHexString:config[@"changeBtnTextColor"]],NSFontAttributeName : [UIFont boldSystemFontOfSize:[config[@"changeBtnTextSize"] floatValue]]}];
//		model.changeBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
//				frame.origin.y = [config[@"changeBtnOffsetY"] doubleValue];
//				return frame;
//		};
//
		//协议
		model.checkBoxIsHidden = [config[@"checkBoxIsHidden"] boolValue];;
		model.checkBoxIsChecked = [config[@"checkBoxIsChecked"] boolValue];;
		NSArray *checkImages = config[@"checkBoxImages"];
		if (checkImages.count == 2) {
				model.checkBoxImages = @[[UIImage imageNamed:checkImages[0]],[UIImage imageNamed:checkImages[1]]];
		}
		model.checkBoxWH =  [config[@"checkBoxWH"] doubleValue];
		NSArray *one = config[@"privacyOne"];
		NSArray *two = config[@"privacyTwo"];
		NSArray *three = config[@"privacyThree"];
		if (one.count == 2) {
				model.privacyOne = one;
		}
		if (two.count == 2) {
				model.privacyTwo = two;
		}
		if (three.count == 2) {
				model.privacyThree = three;
		}
		model.privacyPreText = config[@"privacyPreText"];
		model.privacySufText = config[@"privacySufText"];
		model.privacyOperatorPreText = config[@"privacyOperatorPreText"];
        model.privacyOperatorSufText = config[@"privacyOperatorSufText"];
		
		int  alignment = [config[@"privacySufText"] intValue];
		switch (alignment) {
				case 1:
						model.privacyAlignment = NSTextAlignmentLeft;
						break;
				case 2:
						model.privacyAlignment = NSTextAlignmentRight;
						break;
				case 3:
						model.privacyAlignment = NSTextAlignmentCenter;
						break;
				default:
						break;
		}
        model.privacyAlignment = NSTextAlignmentCenter;
		model.privacyFont = [UIFont boldSystemFontOfSize:[config[@"privacyTextSize"] floatValue]];
		NSArray *colors = config[@"privacyColors"];
		model.privacyColors = @[[self colorWithHexString:colors[0]], [self colorWithHexString:colors[1]]];
		model.privacyFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
				frame.origin.x = [config[@"privacyLRPadding"] intValue];
				frame.origin.y = ((int)screenSize.height *0.7 +60);
                frame.size.width=((int)screenSize.width)-[config[@"privacyLRPadding"] intValue]*2;
				return frame;
		};
        // 新增的参数
        NSInteger privacyAlignment=[config[@"privacyAlignment"] intValue];
        BOOL tipsTextHidden=[config[@"tipsTextHidden"] boolValue];
        NSString *tipsText = config[@"tipsText"];
        NSString *tipsTextColor = config[@"tipsText"];
        NSInteger tipsTextSize=[config[@"tipsTextSize"] intValue];
        NSInteger tipsTextOffsetY=[config[@"tipsTextOffsetY"] intValue];
        BOOL otherLoginButtonHidden=[config[@"otherLoginButtonHidden"] boolValue];
    
        UIButton *restsAuthBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [restsAuthBtn setTitle:config[@"changeBtnText"] forState:UIControlStateNormal];
        restsAuthBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [restsAuthBtn setTitleColor:[UIColor colorWithRed:((float) 29 / 255.0f) green:((float) 210 / 255.0f) blue:((float) 193 / 255.0f) alpha:1.0f] forState:UIControlStateNormal];
    
        restsAuthBtn.frame = CGRectMake(10, 10, mainScreenWidth-36, 45);
        UIImage *name1 = [UIImage imageNamed:@"not_login_button_image"];
        restsAuthBtn.backgroundColor = [UIColor colorWithRed:((float) 242 / 255.0f) green:((float) 242 / 255.0f) blue:((float) 242 / 255.0f) alpha:1.0f];
        //设置圆角
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:restsAuthBtn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20,20)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = restsAuthBtn.bounds;
        maskLayer.path = maskPath.CGPath;
        restsAuthBtn.layer.mask = maskLayer;


        
        UITextView *tipsTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 50, [UIScreen mainScreen].bounds.size.width - 20, 50)];
        [tipsTextView setEditable:NO];
        UIImage *wcImage = [UIImage imageNamed:@"fluat_wechat_login_logo"];//微信登录图标
        UIImageView *wcView =[[UIImageView alloc] initWithImage:wcImage];
        UIImage *appleImage = [UIImage imageNamed:@"fluat_apple_login_logo"];//苹果登录图标
        UIImageView *appleView =[[UIImageView alloc] initWithImage:appleImage];
        if(!tipsTextHidden){
            tipsTextView.text=tipsText;
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:tipsTextSize], NSParagraphStyleAttributeName:paragraphStyle};
            tipsTextView.attributedText = [[NSAttributedString alloc]initWithString: tipsTextView.text attributes:attributes];
            tipsTextView.textAlignment = NSTextAlignmentCenter;
            tipsTextView.textColor = [UIColor colorWithRed:((float) 6 / 255.0f) green:((float) 6 / 255.0f) blue:((float) 6 / 255.0f) alpha:1.0f];
            tipsTextView.backgroundColor=[UIColor whiteColor];
            tipsTextView.alpha=0.3;
        }
        if(!otherLoginButtonHidden){
            wcView.frame = CGRectMake(10, 10, 36, 36);
            wcView.userInteractionEnabled = YES;
            wcView.alpha = 1.0;
            wcView.hidden = NO;
            appleView.frame = CGRectMake(10, 10, 36, 36);
            appleView.userInteractionEnabled = YES;
            appleView.alpha = 1.0;
            appleView.hidden = NO;
            [wcView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:target
                                                                             action:wxSelector]]; // 设置手势
            [appleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:target action:appleSelector]]; // 设置手势
            
            [restsAuthBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:target action:restsClick]];
        }
        //自定义布局添加
        model.customViewBlock = ^(UIView * _Nonnull superCustomView) {
            if(!otherLoginButtonHidden) [superCustomView addSubview: wcView];
            if(!otherLoginButtonHidden) [superCustomView addSubview: appleView];
            if(!tipsTextHidden) [superCustomView addSubview: tipsTextView];
            if(!otherLoginButtonHidden) [superCustomView addSubview: restsAuthBtn];
            
        };
        model.customViewLayoutBlock = ^(
          CGSize screenSize,       /// 全屏参数
          CGRect contentViewFrame, /// contentView参数
          CGRect navFrame,         /// 导航参数
          CGRect titleBarFrame,    /// title参数
          CGRect logoFrame,        /// logo区域参数
          CGRect sloganFrame,      /// slogan参数
          CGRect numberFrame,      /// 号码处参数
          CGRect loginFrame,       /// 登录按钮处的参数
          CGRect changeBtnFrame,   /// 切换到其他的参数
          CGRect privacyFrame      /// 协议区域的参数
        ) {
            NSInteger screenWidth = screenSize.width;
            NSInteger screenHeight = screenSize.height;
            
            if(!otherLoginButtonHidden) wcView.frame = CGRectMake(screenWidth/2+36, screenHeight*0.7, 36, 36);
            if(!otherLoginButtonHidden) appleView.frame = CGRectMake(screenWidth/2-36-27,screenHeight*0.7, 36, 36);
            
            if(!tipsTextHidden){
                NSInteger tipsTextViewWidth=(int)tipsTextView.frame.size.width;
                NSInteger tipsTextViewHeight=(int)tipsTextView.frame.size.height;
                tipsTextView.frame = CGRectMake((screenWidth-tipsTextViewWidth)/2,tipsTextOffsetY-10, tipsTextViewWidth, tipsTextViewHeight);
            }
            
            if(!otherLoginButtonHidden) restsAuthBtn.frame =  CGRectMake(18, tipsTextOffsetY-60, screenWidth-32, 50);
        };
		
		return model;
}

///微信登录
+(void)wcLogin:(UITapGestureRecognizer *)sender{
    NSLog(@"wcLogin");

}

///苹果登录
+(void)appleLogin:(UITapGestureRecognizer *)sender{
    NSLog(@"appleLogin");

}

+ (UIColor *)colorWithHexString:(NSString *)color{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString hasPrefix:@"#"]){
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6){
        return [UIColor clearColor];
    }
     
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
     
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:1];
}

@end
